package com.lemon.cloud.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CreateCache;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lemon.cloud.admin.mapper.RoleApplicationMapper;
import com.lemon.cloud.admin.service.RoleApplicationService;
import com.lemon.cloud.comm.constants.CacheConstants;
import com.lemon.cloud.comm.po.RoleApplication;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Set;

@Service
public class RoleApplicationServiceImpl extends ServiceImpl<RoleApplicationMapper, RoleApplication> implements RoleApplicationService{

    @CreateCache(name = CacheConstants.ROLE_DATA_SCOPE,expire = 8 * 60 * 60)
    private Cache<String, RoleApplication> roleScopeCache;

    @Override
    public RoleApplication getDataScope(String roleCode) {
        if (StrUtil.isEmpty(roleCode)){
            return null;
        }
        return roleScopeCache.computeIfAbsent(roleCode, (s) -> {
            return baseMapper.getDataScope(roleCode);

        });
    }

    @Override
    public boolean saveBatchRoleApp(Set<String> roleCodes, Collection<RoleApplication> entityList) {
        roleScopeCache.removeAll(roleCodes);
        return baseMapper.saveBatchRoleApp(entityList);
    }
}
